package cn.schoolwow.quickdao.module.database.dcl.kit;

import cn.schoolwow.quickdao.domain.database.dcl.DataBaseUser;
import cn.schoolwow.quickdao.domain.database.dcl.GrantOption;
import cn.schoolwow.quickdao.module.database.dcl.service.*;
import cn.schoolwow.quickdao.module.database.parent.kit.DatabaseDAOImpl;
import cn.schoolwow.quickflow.QuickFlow;
import cn.schoolwow.quickflow.domain.FlowContext;

import java.util.List;

public class DatabaseControlImpl extends DatabaseDAOImpl implements DatabaseControl {

    public DatabaseControlImpl(QuickFlow quickFlow) {
        super(quickFlow);
    }

    @Override
    public List<String> getUserNameList() {
        FlowContext flowContext = quickFlow.executeFlow(new GetUserNameListFlow());
        List<String> userNameList = (List<String>) flowContext.getData("userNameList");
        return userNameList;
    }

    @Override
    public boolean hasUserName(String username) {
        List<String> userNameList = getUserNameList();
        for(String username1:userNameList){
            if(username1.equalsIgnoreCase(username)){
                return true;
            }
        }
        return false;
    }

    @Override
    public void createUser(DataBaseUser dataBaseUser) {
        quickFlow.startFlow(new CreateDatabaseUserFlow())
                .putReturnData("dataBaseUser", dataBaseUser)
                .execute();
    }

    @Override
    public void modifyPassword(String username, String newPassword) {
        quickFlow.startFlow(new ModifyPasswordFlow())
                .putReturnData("username", username)
                .putReturnData("newPassword", newPassword)
                .execute();
    }

    @Override
    public void deleteUser(DataBaseUser dataBaseUser) {
        quickFlow.startFlow(new DeleteDatabaseUserFlow())
                .putReturnData("dataBaseUser", dataBaseUser)
                .execute();
    }

    @Override
    public void grant(GrantOption grantOption) {
        quickFlow.startFlow(new GrantFlow())
                .putReturnData("grantOption", grantOption)
                .execute();
    }

    @Override
    public void createUserAndGrant(GrantOption grantOption) {
        quickFlow.startFlow(new CreateDatabaseUserAndGrantFlow())
                .putReturnData("grantOption", grantOption)
                .execute();
    }

    @Override
    public void revoke(GrantOption grantOption) {
        quickFlow.startFlow(new RevokeFlow())
                .putReturnData("grantOption", grantOption)
                .execute();
    }

    @Override
    public void flushPrivileges() {
        quickFlow.executeFlow(new FlushPrivilegesFlow());
    }
}